package com.csii.autocode.utils;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReadFile {
    private static final Logger logger = LoggerFactory.getLogger(ReadFile.class);

    public static List<File> getFileList(File paramFile) throws IOException {
        ArrayList<File> arrayList = new ArrayList();
        addFileToList(paramFile, arrayList);
        Collections.sort(arrayList, (param1File1, param1File2) -> param1File1.getAbsolutePath().compareTo(param1File2.getAbsolutePath()));
        return arrayList;
    }

    public static void addFileToList(File paramFile, List<File> paramList) throws IOException {
        logger.debug("---------dir------------path: " + paramFile.getPath() + " -- isHidden --: " + paramFile.isHidden() + " -- isDirectory --: " + paramFile.isDirectory());
        if (!paramFile.isHidden() && paramFile.isDirectory() && !isContain(paramFile)) {
            File[] arrayOfFile = paramFile.listFiles();
            for (byte b = 0; b < arrayOfFile.length; b++)
                addFileToList(arrayOfFile[b], paramList);
        } else if (!isEndWith(paramFile) && !isContain(paramFile)) {
            paramList.add(paramFile);
        }
    }

    //字符串截取
    public static String cutString(File paramFile1, File paramFile2) {
        if (paramFile1.equals(paramFile2))
            return "";
        if (paramFile1.getParentFile() == null)
            return paramFile2.getAbsolutePath().substring(paramFile1.getAbsolutePath().length());
        return paramFile2.getAbsolutePath().substring(paramFile1.getAbsolutePath().length() + 1);
    }

    //判断是否为文件
    public static boolean isfile(File paramFile) {
        if (paramFile.isDirectory())
            return false;
        return isBlank(paramFile.getName());
    }

    public static boolean isBlank(String paramString) {
        if (StrUtil.isBlank(cutString(paramString)))
            return false;
        return true;
    }

    public static String cutString(String paramString) {
        if (paramString == null)
            return null;
        int i = paramString.indexOf(".");
        if (i == -1)
            return "";
        return paramString.substring(i + 1);
    }

    public static File getFile(String paramString) {
        if (paramString == null)
            throw new IllegalArgumentException("file must be not null");
        File file = new File(paramString);
        makeDirs(file);
        return file;
    }

    public static void makeDirs(File paramFile) {
        if (paramFile.getParentFile() != null)
            paramFile.getParentFile().mkdirs();
    }

    private static List<String> list1 = new ArrayList<>();

    private static List<String> list2 = new ArrayList<>();

    static {
        list1.add(".svn");
        list1.add("CVS");
        list1.add(".cvsignore");
        list1.add(".copyarea.db");
        list1.add("SCCS");
        list1.add("vssver.scc");
        list1.add(".DS_Store");
        list1.add(".git");
        list1.add(".gitignore");
        list2.add(".ftl");
    }

    private static boolean isContain(File paramFile) {
        for (byte i = 0; i < list1.size(); i++) {
            if (paramFile.getName().equals(list1.get(i)))
                return true;
        }
        return false;
    }

    private static boolean isEndWith(File paramFile) {
        for (byte i = 0; i < list2.size(); i++) {
            if (paramFile.getName().endsWith(list2.get(i)))
                return true;
        }
        return false;
    }
}
